package com.webdiy.dfss.action;

import com.sys.plat.bean.PageBean;
import com.sys.plat.util.HttpUtil;
import com.sys.plat.web.core.ActionContext;
import com.sys.plat.web.core.BaseAction;
import com.sys.tool.HttpClientTool;
import com.sys.tool.StringTool;
import com.sys.web.util.SystemUtil;
import com.webdiy.dao.model.DfssPlan;
import com.webdiy.dao.model.DfssUserInfo;
import com.webdiy.dfss.form.PlanForm;
import com.webdiy.dfss.iface.PlanService;
import com.webdiy.dfss.impl.PlanServiceImpl;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: mabo
 * Date: 2009-11-25
 * Time: 16:26:29
 * To change this template use File | Settings | File Templates.
 */
public class PlanAction extends BaseAction {
    private PlanService getService() {
        return (PlanService) getService("PlanServiceImpl");
    }

    private PlanForm getForm() {
        return (PlanForm) ActionContext.getForm();
    }


    public String find() {
        DfssUserInfo u = (DfssUserInfo) ActionContext.getRequest().getSession().getAttribute("dfssUserInfo");
        PageBean pageBean = createPageBean(getForm());
        Map map = pageBean.getFiltermap();
        map.put("userId", u.getId());
        pageBean.setFiltermap(map);
        getService().findPlan(pageBean);
        pageBean.setToWeb();
        HttpUtil.saveSearchParemeter();
        HttpUtil.reqSetAttr("u", u);


        String xyxx = (String) HttpUtil.sessionGetAttr("xyxx");
        if (null == xyxx || xyxx.equals("")) {
HttpClientTool hct = new HttpClientTool();
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            list.add(new BasicNameValuePair("edtStudentID", u.getUserCode()));
            list.add(new BasicNameValuePair("edtPassword", u.getUserPassWord()));
            list.add(new BasicNameValuePair("__EVENTTARGET", "btnSearch"));
            list.add(new BasicNameValuePair("__VIEWSTATE", "dDwtMzQ5NDE2NDMwO3Q8O2w8aTwyPjs+O2w8dDw7bDxpPDk+Oz47bDx0PHA8bDx2YWx1ZTs+O2w86KejICDplIE7Pj47Oz47Pj47Pj47PohG/2GfWW4iDo4FkpwpstcnUecB"));
            hct.post("http://yueche.dfss.cn/web/cmp/login.aspx", list);

            hct.setUrl("http://yueche.dfss.cn/web/cmp/yc.aspx?StudentID=" + u.getUserCode());

            String htmlBody = hct.getInnerHTML();

            StringTool st = new StringTool();
            st.setMatcher(htmlBody);
            st.setPattern("<tr class=\"iTr\">([^.|.]*?)学员编号([^.|.]*?)</table>");

            if (st.isFind()) {
                xyxx = st.getValue(0);
                xyxx = "<table >" + xyxx;
            } else {
                xyxx = "东方时尚网络约车系统登录失败，或者系统处于关闭状态";
            }
            HttpUtil.sessionSetAttr("xyxx", xyxx);
        }


        HttpUtil.reqSetAttr("xyxx", xyxx);
        return LIST;
    }

    public String add() {
        HttpUtil.reqSetAttr("p", getForm().getP());
        return EDIT;
    }

    public String update() {
        DfssUserInfo u = (DfssUserInfo) ActionContext.getRequest().getSession().getAttribute("dfssUserInfo");
        if (getForm().getRdate().equals("2")) {
            getForm().getP().setPlanDate(getForm().getWeeks());
        }

        DfssPlan p = getForm().getP();
        p.setUserId(u.getId());

        if (p.getToday() == 0) {
            p.setToday(2);
        }
        
        getService().savePlan(p);
        return find();
    }

    public String del() {
        getService().delPlan(getForm().getIds());
        return find();
    }

    public String start() {
        stop();
        HttpUtil.sessionSetAttr("dfssUserInfo", getService().start((DfssUserInfo) HttpUtil.sessionGetAttr("dfssUserInfo")));
        return find();
    }

    public String stop() {
        HttpUtil.sessionSetAttr("dfssUserInfo", getService().stop((DfssUserInfo) HttpUtil.sessionGetAttr("dfssUserInfo")));
        return find();
    }
}
